package com.dfbz.demo;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo03_序列化案例 {


    public static void main(String[] args) throws Exception{

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("books.ser"));

        List<Book> books = (List<Book>) ois.readObject();

        for (Book book : books) {
            System.out.println(book);
        }

        ois.close();
    }
    public static void test() throws Exception{

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("books.ser"));

        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book("西游记","吴承恩"));
        books.add(new Book("红楼梦","曹雪芹"));
        books.add(new Book("水浒传","施耐庵"));
        books.add(new Book("三国演义","罗贯中"));

        oos.writeObject(books);
        oos.close();

    }
}

class Book implements Serializable {
    private String name;
    private String author;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Book() {
    }

    public Book(String name, String author) {
        this.name = name;
        this.author = author;
    }
}
